package cn.wolfcode.luowowo.mgrsite.Listener;

import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.cache.service.IStrategyStatisRedisService;
import cn.wolfcode.luowowo.cache.service.ITravelStatisRedisService;
import cn.wolfcode.luowowo.cache.util.RedisKeys;
import cn.wolfcode.luowowo.cache.vo.StrategyStatisVO;
import cn.wolfcode.luowowo.cache.vo.TravelStatisVO;
import com.alibaba.dubbo.config.annotation.Reference;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Set;

/**
 * @author 猫教授
 * @date 2019/8/14 21:39
 */

/**
 * spring应用容器启动监听器
 * 监听类型由泛型指定
 * 所有类型的监听都是实现 ApplicationListener 类
 * 启动时间的监听泛型为 ContextRefreshedEvent
 */
@Component
public class RedisDataInitListener implements ApplicationListener<ContextRefreshedEvent> {
    @Reference
    private IStrategyDetailService strategyDetailService;
    @Reference
    private IStrategyStatisRedisService strategyStatisRedisService;
    @Reference
    private ITravelStatisRedisService travelStatisRedisService;

    @Override
    public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) {
        //这里我们要做的就是把数据库的数据同步到redis里面
        //这里统一先同步攻略相关的数据
        //第一步我们先拿出所有攻略的列表数据
       /* System.out.println("开始同步");
        List<StrategyDetail> list = strategyDetailService.list();
        StrategyStatisVO statisVO = new StrategyStatisVO();
        list.forEach((Detail)->{
            //先进行判断看里面有没有,没有再同步,有就不同步
            String key = RedisKeys.STRATEGY_STATIC_VO.join(Detail.getId().toString());
            boolean exists=strategyStatisRedisService.existsKey(key);
            if (exists==false) {
                System.out.println(Detail.getId());
                try {
                    BeanUtils.copyProperties(statisVO,Detail);
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                }
                //有一些数据需要自己修改进去
                statisVO.setDestId(Detail.getDest().getId());
                statisVO.setDestName(Detail.getDest().getName());
                statisVO.setStrategyId(Detail.getId());
                //TODO 评论数待定
                strategyStatisRedisService.setStrategyStatisVO(statisVO);
            }
            //这里是重mysql初始化数据的
//            boolean hotHasInZset=strategyStatisRedisService.hasInZset(RedisKeys.STRATEGY_HOT_SORT.getPrefix(),Detail.getId().toString());
//            if (!hotHasInZset){
//                strategyStatisRedisService.addSortNum(RedisKeys.STRATEGY_HOT_SORT.getPrefix(),Detail.getId(),Detail.getViewnum()+Detail.getReplynum());
//            }
//            boolean commendHasInZset=strategyStatisRedisService.hasInZset(RedisKeys.STRATEGY_COMMEND_SORT.getPrefix(),Detail.getId().toString());
//            if (!commendHasInZset) {
//                strategyStatisRedisService.addSortNum(RedisKeys.STRATEGY_COMMEND_SORT.getPrefix(),Detail.getId(),Detail.getFavornum()+Detail.getThumbsupnum());
//            }
           
        });*/
        //System.out.println("同步结束");
        //这里是从vo初始化数据
      /*  Set<String> keyList = strategyStatisRedisService.getKeyList(RedisKeys.STRATEGY_STATIC_VO.join("*"));
        for (String key : keyList) {
            System.out.println("同步开始");
            StrategyStatisVO vo = strategyStatisRedisService.getVoByRedisKey(key);
            boolean hotHasInZset=strategyStatisRedisService.hasInZset(RedisKeys.STRATEGY_HOT_SORT.getPrefix(),vo.getStrategyId().toString());
            if (!hotHasInZset){
                strategyStatisRedisService.addSortNum(RedisKeys.STRATEGY_HOT_SORT.getPrefix(),vo.getStrategyId(),vo.getViewnum()+vo.getReplynum());
            }
            boolean commendHasInZset=strategyStatisRedisService.hasInZset(RedisKeys.STRATEGY_COMMEND_SORT.getPrefix(),vo.getStrategyId().toString());
            if (!commendHasInZset) {
                strategyStatisRedisService.addSortNum(RedisKeys.STRATEGY_COMMEND_SORT.getPrefix(),vo.getStrategyId(),vo.getFavornum()+vo.getThumbsupnum());
            }
            System.out.println("同步结束");
        }*/
        Set<String> keyList = travelStatisRedisService.getKeyList(RedisKeys.TRAVEL_STATIC_VO.join("*"));
        for (String key : keyList) {
            System.out.println("同步开始");
            TravelStatisVO vo = travelStatisRedisService.getVoByRedisKey(key);
            boolean hotHasInZset=travelStatisRedisService.hasInZset(RedisKeys.TRAVEL_HOT_SORT.getPrefix(),vo.getTravelId().toString());
            if (!hotHasInZset){
                travelStatisRedisService.addSortNum(RedisKeys.TRAVEL_HOT_SORT.getPrefix(),vo.getTravelId(),vo.getViewnum()+vo.getReplynum());
            }
            boolean commendHasInZset=travelStatisRedisService.hasInZset(RedisKeys.TRAVEL_COMMEND_SORT.getPrefix(),vo.getTravelId().toString());
            if (!commendHasInZset) {
                travelStatisRedisService.addSortNum(RedisKeys.TRAVEL_COMMEND_SORT.getPrefix(),vo.getTravelId(),vo.getFavornum()+vo.getThumbsupnum());
            }
            System.out.println("同步结束");
        }
    }
}
